package com.ruoyi.project.tool.swagger;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.netflix.discovery.converters.Auto;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.OperatorType;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.SysDept;
import com.ruoyi.project.system.domain.SysOrder;
import com.ruoyi.project.system.es.SysDeptRepository;
import com.ruoyi.project.system.service.ICartService;
import com.ruoyi.project.system.service.SysOrderDetailService;
import com.ruoyi.project.system.service.SysOrderService;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mobile")
public class CartController {

    @Autowired
    private ICartService cartService;

    @Autowired
    private SysOrderService sysOrderService;

    @Autowired
    private SysOrderDetailService sysOrderDetailService;


    @Autowired
    private SysDeptRepository sysDeptRepository;

    /**
     * 校验订单数据
     */
    @PostMapping(value = "/cart")
    @Log(title = "校验订单数据", operatorType = OperatorType.MOBILE)
    public AjaxResult category(@RequestBody Map map)
    {
        return cartService.checkData(map);
    }


    /**
     * 生成购物车数据
     */
    @PostMapping(value = "/createOrder")
    @Log(title = "生成订单", operatorType = OperatorType.MOBILE)
    public AjaxResult createOrder(@RequestBody Map map)
    {
        return cartService.createOrder(map);
    }

    /**
     * 生成购物车数据
     */
    @GetMapping(value = "/cart/remarks")
    @Log(title = "订单快捷备注", operatorType = OperatorType.MOBILE)
    public AjaxResult getRemark()
    {
        JSONArray array=new JSONArray();
        array.add("不要辣");
        array.add("少点辣");
        array.add("多点辣");
        array.add("非常辣");

        JSONArray array1=new JSONArray();
        array1.add("不要香菜");
        array1.add("要香菜");
        JSONArray array2=new JSONArray();
        array2.add("多点醋");
        array2.add("少点醋");
//        JSONArray array3=new JSONArray();
//        array3.add("去冰");
//        array3.add("少冰");
        JSONArray returnJSOn=new JSONArray();
        returnJSOn.add(array);
        returnJSOn.add(array1);
        returnJSOn.add(array2);
//        returnJSOn.add(array3);
        return AjaxResult.success(returnJSOn);
    }


    @RequestMapping(value = "/orders", method = RequestMethod.GET)
    @Log(title = "订单列表", operatorType = OperatorType.MOBILE)
    public AjaxResult listShop(@RequestParam(value="offset",required = false) String offset) {
        List<SysOrder> list= sysOrderService.queryAllByLimit(Integer.valueOf(offset),10,SecurityUtils.getLoginUser().getUser().getUserId());
        if(!list.isEmpty()){
            for(SysOrder sysOrder:list){
                SysDept sysDept=sysDeptRepository.findById(Integer.valueOf(sysOrder.getDeptId()+"")).get();
                sysOrder.setDeptName(sysDept.getDeptName());
                sysOrder.setSysDeptImg(sysDept.getFileList().get(0).getUrl());
                sysOrder.setDescription(sysDept.getDescription());
                sysOrder.setPromotionInfo(sysDept.getPromotionInfo());
                sysOrder.setDetails( sysOrderDetailService.queryByOrderId(sysOrder.getOrderId()));
            }
        }
        return AjaxResult.success(list);
    }

    @RequestMapping(value = "/orders/{orderId}", method = RequestMethod.GET)
    @Log(title = "订单详情", operatorType = OperatorType.MOBILE)
    public AjaxResult queryDetail(@PathVariable(value="orderId",required = true) Long orderId) {
        SysOrder sysOrder= sysOrderService.queryById(orderId);
        if(null!=sysOrder){
            SysDept sysDept=sysDeptRepository.findById(Integer.valueOf(sysOrder.getDeptId()+"")).get();
            sysOrder.setDeptName(sysDept.getDeptName());
            sysOrder.setSysDeptImg(sysDept.getFileList().get(0).getUrl());
            sysOrder.setDetails( sysOrderDetailService.queryByOrderId(sysOrder.getOrderId()));
        }
        return AjaxResult.success(sysOrder);
    }

}
